package com.byd.android.MusicPlayer;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.byd.android.agreement.message.Music;
import com.byd.android.mediaplayer.R;

import java.util.List;

public class MusicAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private List<Music> list;

    public MusicAdapter(Context context, List<Music> list) {
        this.context = context;

        layoutInflater = LayoutInflater.from(context);
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int positon) {
        return this.list.get(positon);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class MusicHolder {
        TextView MusicId ;
        TextView MusicName ;
        TextView MusicArtist;
        TextView MusicDurtion;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // Music music = list.get(position);
        if(convertView == null){
            convertView = layoutInflater.inflate(R.layout.music_item, null);
        }

        MusicHolder holder;

        if (convertView.getTag() != null) {
            holder = (MusicHolder) convertView.getTag();
        } else {
            holder = new MusicHolder();
            holder.MusicId = (TextView) convertView.findViewById(R.id.item_number);
            holder.MusicName = (TextView) convertView.findViewById(R.id.music_name);
            holder.MusicArtist = (TextView) convertView.findViewById(R.id.artist);
            holder.MusicDurtion = (TextView)convertView.findViewById(R.id.durtion);
        }

        convertView.setTag(holder);

        int msec = list.get(position).getDuration();

        holder.MusicId.setText(list.get(position).getId()+1+"");
        holder.MusicName.setText(list.get(position).getMusicName()+"");
        holder.MusicArtist.setText(list.get(position).getArtist()+"");
        holder.MusicDurtion.setText(((msec)/1000)/60+":"+((msec)/1000)%60+"");

        return convertView;
    }
}

